#!/bin/bash -e
# merge first bam and complementary bam to re-analysis
function info() {
echo Usage: `basename $0` '[sigGdt] in1.bam in2.bam'
exit 2
}

args="`basename $0` $*"
while getopts  ":p:f:s:i:gGd:t:" opt; do
	case  $opt  in
		p) out_prefix=$OPTARG;;
		f) suffix=$OPTARG;;
        s) sample_name=$OPTARG;;
        i) target=$OPTARG;;
        g) single_cell=T;;
        G) somatic=T;;
        d) desti=$OPTARG;;
        t) run_continue_dir=$OPTARG;;
		*) info;;
	esac
done
shift $(($OPTIND - 1))

if [ $# -lt 2 ]; then info; fi


. $var

mnt=mnt.sh
path_abs=path_abs.sh
makedir=makedir.sh
merge_bam=merge_bam.sh
markdup=deldup.sh
fastqc=fastqc.sh
cal0=ug.sh
summ=summ.sh
anno=anno_110416.sh
format_simple=format_top_simple.sh
format=format_top_0222.sh
rename=file_rename_0222.sh



bam1=$($path_abs $1)
bam2=$($path_abs $2)

echo $sample_name
echo $target
echo $bam1 $bam2


if test -z "$run_continue_dir"; then
run_continue_dir=`$makedir -p$sample_name` && cd $run_continue_dir
else 
cd $run_continue_dir
fi

$mnt `basename $0`.`basename $run_continue_dir` & # `basename $run_continue_dir` for strip / if exist in dir's end.

if
cat $pid_log|grep "$merge_bam $bam1 $bam2 done" >/dev/null; then
echo $merge_bam done
else
$merge_bam -p$out_prefix $bam1 $bam2
fi


if
cat $pid_log|grep "$fastqc $out_prefix.merge.bam done" >/dev/null; then
echo $fastqc done
else
$fastqc -s$sample_name $out_prefix.merge.bam &
fi



if
cat $pid_log|grep "$cal0 $out_prefix.merge.bam done" >/dev/null; then
echo $cal0 $out_prefix.merge.bam done
else
	if test -n "$somatic"; then
		echo somatic or low coverage
        if test -n "$single_cell"; then
            echo single cell
            $cal0 -G1 -p$out_prefix -i$target $out_prefix.merge.bam
        else
            $cal0 -G -p$out_prefix -i$target $out_prefix.merge.bam
		fi
	else
        if test -n "$single_cell"; then
            $cal0 -1 -p$out_prefix -i$target $out_prefix.merge.bam
        else
            $cal0 -p$out_prefix -i$target $out_prefix.merge.bam
        fi
	fi
fi


if
cat $pid_log|grep "$summ $out_prefix.merge.bam $target done" >/dev/null; then
echo $summ done
else
$summ -g -s$sample_name $out_prefix.merge.bam $target #can not background

ln $out_prefix.gene_summ.txt gene_summ.txt
fi



if
cat $pid_log|grep "$anno $out_prefix.snp.vcf $out_prefix.indel.vcf done" >/dev/null; then
echo $anno done
else

if test -n "$somatic"; then
	echo somatic
	if test -n "$single_cell"; then
		echo;echo;echo single_cell
		$anno -m -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	else
		$anno -m -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	fi
else
	echo germline
	if test -n "$single_cell"; then
		echo;echo;echo single_cell
		$anno -g -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	else
		$anno -l $target $out_prefix.snp.vcf $out_prefix.indel.vcf
	fi
fi

fi


if
cat $pid_log|grep "$format_simple done" >/dev/null; then
echo $format_simple done
else
$format_simple &
fi


if
cat $pid_log|grep "$format $out_prefix.high.vcf $out_prefix.low.vcf done" >/dev/null; then
echo $format done
else
$format $out_prefix.high.vcf $out_prefix.low.vcf
fi


wait



if
cat $pid_log|grep "$rename $sample_name done" >/dev/null; then
echo $rename done
else
$rename $sample_name
fi



echo;echo; echo desti: $desti

bk.sh -t$desti ${sample_name}*simple_info*.xls ${sample_name}*summary*.xls ${sample_name}*stat*.xls ${sample_name}*reads*.xls ${sample_name}*.pdf ${sample_name}*.html .log* ${sample_name}*deleterious.txt ${sample_name}*drugs-prediction*



. $cmd_done